{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "48cc0c40-c08e-4cac-83da-96f893356eff",
   "metadata": {},
   "source": [
    "## Practice 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f34169a0-4f9f-41b8-a665-17e4e45c84d0",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0dc33c30-1abb-4020-959f-e8642a743070",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "### No.1\n",
    "\n",
    "创建一个长度为 10 的一维全为 0 的 ndarray 数组，然后让第 5 个元素等于 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "24545fea-40db-4889-a615-126196144950",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.zeros(shape = 10)\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ec35db87-8757-4b7a-a77e-bfee36710b38",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0., 0., 0., 0., 1., 0., 0., 0., 0., 0.])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr[4] = 1\n",
    "arr"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8c1ebf15-23c8-4a80-9c50-5fa836f2f15f",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "### No.2\n",
    "\n",
    "创建一个元素为从 10 到 49（包含 49）的 ndarray 数组，间隔是 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f7a25995-5c83-48a8-876a-783eccada30e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,\n",
       "       27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,\n",
       "       44, 45, 46, 47, 48, 49])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(10, 50)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b22275c6-6565-4f45-ade0-7acad399165e",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "### No.3\n",
    "\n",
    "将第 2 题中数组的所有元素位置反转"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "628c9d0d-cddf-43c6-9bd1-4dab49ea049a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33,\n",
       "       32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16,\n",
       "       15, 14, 13, 12, 11, 10])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.arange(10, 50)\n",
    "arr[::-1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e501d764-f17d-4d9d-a9df-a075937fa759",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "### No.4\n",
    "\n",
    "使用 np.random.random 创建一个 10*10 的 nparray 数组，并打印出最大和最小元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e4b9110d-3126-4864-a529-aa1116b3422f",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr = np.random.random(size = (10, 10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "d2ae698d-45d8-4057-8b1f-b07c40ffeed3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9900364297050966"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr.max()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "4ed2f18a-ab30-483c-b180-6f35c6e3f29e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.006689154020528831"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr.min()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9eb88e76-fffc-4dec-9f03-cef9562f96dc",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "### No.5\n",
    "\n",
    "创建一个 10*10 的 ndarray 数组，矩阵边界为 1，里边全为 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "22a860f3-928a-40da-8237-db04dcd52afb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n",
       "       [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],\n",
       "       [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],\n",
       "       [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],\n",
       "       [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],\n",
       "       [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],\n",
       "       [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],\n",
       "       [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],\n",
       "       [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],\n",
       "       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], dtype=int16)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.zeros(shape = (10, 10), dtype = np.int16)\n",
    "arr[0] = 1\n",
    "arr[9] = 1\n",
    "arr[:, 0] = 1\n",
    "arr[:, 9] = 1\n",
    "arr"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c83cf9c7-8189-4b8a-9a7b-e2d1867afc69",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "### No.6\n",
    "\n",
    "创建一个每一行都是从 0 到 4 的 5*5 矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "37f4eed4-89e7-4ca5-b597-01e018233567",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 3, 4],\n",
       "       [0, 1, 2, 3, 4],\n",
       "       [0, 1, 2, 3, 4],\n",
       "       [0, 1, 2, 3, 4],\n",
       "       [0, 1, 2, 3, 4]], dtype=int16)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.zeros(shape = (5,5), dtype = np.int16)\n",
    "\n",
    "for i in range(1, 5):\n",
    "    arr[:, i] = arr[:, i-1] + 1\n",
    "\n",
    "arr"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6ae604c5-1b70-4f21-972b-d6d257133798",
   "metadata": {},
   "source": [
    "### No.7\n",
    "\n",
    "- 创建一个范围在 (0,1) 之间的长度为 12 的等差数列\n",
    "- 创建 `[1,2,4,8,16,32,64,128,256,512,1024]` 的等比数列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "53fd5067-c018-4188-b2c5-52a657ad7ccb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.        , 0.09090909, 0.18181818, 0.27272727, 0.36363636,\n",
       "       0.45454545, 0.54545455, 0.63636364, 0.72727273, 0.81818182,\n",
       "       0.90909091, 1.        ])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.linspace(0, 1, num = 12)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "cd643c63-103a-4530-ae37-29837b2b8be5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([   1.,    2.,    4.,    8.,   16.,   32.,   64.,  128.,  256.,\n",
       "        512., 1024.])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.set_printoptions(suppress=True)\n",
    "np.logspace(0, 10, num = 11, base = 2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b4a99550-bc2c-4afc-a44e-01740bbb993d",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "### No.8\n",
    "\n",
    "创建一个长度为 10 的正态分布数组并排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "c78ef31e-47bf-499d-8cb1-8d111ecce470",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-0.13840038,  0.41812934,  0.81694159,  1.01690969,  1.10797941,\n",
       "       -0.14171018,  0.54807413, -0.05373629,  1.13213248,  0.25924225])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.random.randn(10)\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "6e7c3f75-ecb8-4690-97e0-e1be0adf46ae",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-0.14171018, -0.13840038, -0.05373629,  0.25924225,  0.41812934,\n",
       "        0.54807413,  0.81694159,  1.01690969,  1.10797941,  1.13213248])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr[arr.argsort()]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e54131e4-ff03-49c1-a59c-c50b0e687808",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "### No.9\n",
    "\n",
    "创建一个长度为 10 的随机数组并将最大值替换为 -100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "219851b2-fee3-47aa-a7c4-5a0ca092f0db",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 3, 5, 3, 8, 1, 0, 5, 1, 8])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.random.randint(0, 10, size = 10)\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "72ea80f5-31aa-4bae-ac81-12b1eae1bd1d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([   3,    3,    5,    3, -100,    1,    0,    5,    1, -100])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.where(arr < arr.max(), arr, -100)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e07ac38f-4510-4f0a-aa58-59896e0ab0ca",
   "metadata": {},
   "source": [
    "### No.10\n",
    "\n",
    "如何根据第 3 列大小顺序来对一个 5*5 矩阵排序？（提示：查阅 argsort 方法；根据某一列重新组织行顺序）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "0b65f000-edef-4c1e-bab3-fb27f797d862",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 6, 2, 5, 7],\n",
       "       [5, 5, 8, 1, 6],\n",
       "       [5, 5, 7, 3, 5],\n",
       "       [0, 8, 5, 7, 7],\n",
       "       [7, 1, 4, 6, 5]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.random.randint(0, 10, size=(5,5))\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "135d947d-a224-41f7-93aa-e4b31ac475b4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 4, 3, 2, 1])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sort_idx = arr[:, 2].argsort()\n",
    "sort_idx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "91177fcf-6a85-4db0-b573-7ccc8b830c5e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 6, 2, 5, 7],\n",
       "       [7, 1, 4, 6, 5],\n",
       "       [0, 8, 5, 7, 7],\n",
       "       [5, 5, 7, 3, 5],\n",
       "       [5, 5, 8, 1, 6]])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 按照第三列的大小顺序重新排列所有行\n",
    "arr[sort_idx]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "73f15f15-e9ff-4607-bdc8-712c117ddc11",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
